﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace MyPublish.Connection
{
    public class DataConnection : IDisposable
    {
        public DataConnectionString ConnectionString { get; set; }
        public WebProxy Proxy { get; set; }
        private DataConnectionProvider Provider { get; set; }

        public DataConnection(string connString)
        {
            this.ConnectionString = DataConnectionString.Parse(connString);
        }

        public DataConnection(DataConnectionString connString)
            :this(connString,null)
        {}

        public DataConnection(DataConnectionString connString, WebProxy proxy)
        {
            this.ConnectionString = connString;
            this.Proxy = proxy;
        }

        public IEnumerable<object> ExecuteQuery()
        {
            return Provider.ExecuteQuery();
        }

        public IEnumerable<object> ExecuteQuery(object data)
        {
            return Provider.ExecuteQuery(data);
        }

        public void Open()
        {
            Provider = 
                DataConnectionFactory.Instance.CreateConnectionProvider(ConnectionString.Provider);
            Provider.ConnectionString = ConnectionString;
            Provider.Proxy = Proxy;
        }

        public void Close()
        {
            Provider = null;
        }

        public void Dispose()
        {
            Close();
        }
    }
}
